﻿using System;
using System.Collections.Generic;
using CodeCamp.Infrastructure.Extensions;

namespace CodeCamp.Models.Logs
{
    public class LogListEntryViewModel
    {
        private const int simplifiedUrlMaxLenDefault = 30;

        public long LogEntryID { get; set; }
        public string Level { get; set; }
        public DateTime LogTimeLocal { get; set; }
        public string MachineName { get; set; }
        public string Message { get; set; }
        public Nullable<int> HeadlineMaxLen { get; set; }
        public string MessageHeadline
        {
            get { return Message.TruncateWithElipses(HeadlineMaxLen.HasValue ? HeadlineMaxLen.Value : 30); }
        }
        public string RemoteIP { get; set; }
        public string RemoteIPNoNull
        {
            get
            {
                if (RemoteIP == null)
                    return string.Empty;
                else if (RemoteIP.Equals("(null)", StringComparison.InvariantCultureIgnoreCase))
                    return string.Empty;
                return RemoteIP;
            }
        }
        public int? ThreadId { get; set; }
        public string URL { get; set; }
        public string UserName { get; set; }
        public string UserNameNoNull
        {
            get
            {
                if (UserName == null)
                    return string.Empty;
                else if (UserName.Equals("(null)", StringComparison.InvariantCultureIgnoreCase))
                    return string.Empty;
                return UserName;
            }
        }

        public string SimplifiedUrl
        {
            get
            {
                if (string.IsNullOrWhiteSpace(URL))
                    return string.Empty;

                if (URL.Equals("(null)", StringComparison.InvariantCultureIgnoreCase))
                    return string.Empty;

                if (URL.Length > SimplifiedUrlMaxLen)
                {
                    int rawTailIdx = URL.Length - (SimplifiedUrlMaxLen - 3);
                    int slashIndex = URL.Substring(rawTailIdx).IndexOf('/');
                    if ((slashIndex >= 0) && ((rawTailIdx + slashIndex) < URL.Length))
                    {
                        string tail = URL.Substring(rawTailIdx + slashIndex);
                        return string.Concat("...", tail);
                    }
                    return string.Concat("...", URL.Substring((URL.Length-SimplifiedUrlMaxLen)+3));
                }

                return URL;
            }

        }
        private Nullable<int> _simplifiedUrlMaxLen;
        public int SimplifiedUrlMaxLen
        {
            get
            {
                if (!_simplifiedUrlMaxLen.HasValue)
                    _simplifiedUrlMaxLen = simplifiedUrlMaxLenDefault;
                return _simplifiedUrlMaxLen.Value;
            }
            set
            {
                if (value <= 0)
                    _simplifiedUrlMaxLen = null;
                else
                    _simplifiedUrlMaxLen = value;
            }
        }

    }
}